package TD3;

import java.util.ArrayList;

public class ListeEntier {
	
	private ArrayList<DoubletComparable> l;
	
	public ListeEntier() {
		l = new ArrayList<DoubletComparable>();
	}
	
	public ArrayList<DoubletComparable> getList(){
		return l;
	}
	public void add(DoubletComparable d){
		l.add(0,d);
		cocktailSort();
	}

	public void cocktailSort(){
		  // `begin` and `end` marks the first and last index to check
		  int begin = -1;
		  int end = l.size() - 1;
		  boolean swapped = true;
		  DoubletComparable dc1, dc2;
		  
		  while(swapped){
		    swapped = false;
		    // increases `begin` because the elements before `begin` are in correct order
		    begin++;
		    
		    for (int i = begin; i < end; i++ ){
		    	dc1 = (DoubletComparable)l.get(i);
		    	dc2 = (DoubletComparable)l.get(i+1);
		    
		      if (dc1.compareTo(dc2) > 0){
		        l.set(i, dc2);
		        l.set(i+1, dc1);
		        swapped = true;
		      }
		    }
		    if (swapped = false){
		      //continue; //or break?
		      break;
		  }
		    swapped = false;
		    // decreases `end` because the elements after `end` are in correct order
		    end--;
		    for (int i = end ; i > begin ; i--){
		    	dc1 = (DoubletComparable)l.get(i-1);
		    	dc2 = (DoubletComparable)l.get(i);
		      
		      if (dc1.compareTo(dc2) > 0){
			       l.set(i, dc2);
			       l.set(i+1, dc1);
		        swapped = true;
		      }
		    }
		}

	}

	public static void main (String[] args){
			
		ListeEntier l = new ListeEntier();
		l.add(new DoubletComparable(1,2));
		l.add(new DoubletComparable(3,3));
		
		
		for(DoubletComparable d : l.getList()){
			System.out.println(d.toString());
		}
		l.add(new DoubletComparable(5,2));
		l.add(new DoubletComparable(6,3));
		for(DoubletComparable d : l.getList()){
			System.out.println(d.toString());
		}
		
	}
	
}
